package com.polaris.live.common.util

import android.app.Service
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Vibrator
import android.os.VibratorManager
import com.blankj.utilcode.util.ActivityUtils

/**
 * SystemUtils
 *
 * @author Created by lucas on 2023/10/24 14:11
 */
object SystemUtils {

    /**
     * 手机系统版本是否等于22
     */
    fun isSdkInt22(): Boolean {
        return Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1
    }

    /**
     * 手机系统版本是否大于等于23
     */
    fun isSdkInt23(): Boolean {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
    }

    /**
     * 手机系统版本是否大于等于24
     */
    fun isSdkInt24(): Boolean {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
    }

    /**
     * 手机系统版本是否大于等于25
     */
    fun isSdkInt25(): Boolean {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1
    }

    /**
     * 手机系统版本是否等于26
     */
    fun equalsSdkInt26(): Boolean {
        return Build.VERSION.SDK_INT == Build.VERSION_CODES.O
    }

    /**
     * 手机系统版本是否大于等于26
     */
    fun isSdkInt26(): Boolean {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
    }

    /**
     * 手机系统版本是否大于等于28
     */
    fun isSdkInt28(): Boolean {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.P
    }

    /**
     * 手机系统版本是否大于等于29
     */
    fun isSdkInt29(): Boolean {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q
    }

    /**
     * 手机系统版本是否大于等于30
     */
    fun isSdkInt30(): Boolean {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.R
    }

    /**
     * 手机系统版本是否大于等于31
     */
    fun isSdkInt31(): Boolean {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
    }

    /**
     * 手机系统版本是否大于等于32
     */
    fun isSdkInt32(): Boolean {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.S_V2
    }

    /**
     * 手机系统版本是否大于等于33
     */
    fun isSdkInt33(): Boolean {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU
    }

    /**
     * 震动一下
     *
     * @param context 上下文
     */
    fun vibrate(context: Context) {
        val vibrator = if (isSdkInt31()) {
            (context.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as? VibratorManager)?.defaultVibrator
        } else {
            context.getSystemService(Service.VIBRATOR_SERVICE) as? Vibrator
        }

        vibrator?.vibrate(longArrayOf(0, 40), -1)
    }

    /**
     * 跳转至应用市场更新
     */
    fun launchAppDetail(appPkg: String, marketPkg: String = "com.android.vending") {
        try {
            if (appPkg.isEmpty()) {
                return
            }

            val uri = Uri.parse("market://details?id=$appPkg")
            val intent = Intent(Intent.ACTION_VIEW, uri)
            if (marketPkg.isNotEmpty()) {
                intent.setPackage(marketPkg)
            }
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            ActivityUtils.startActivity(intent)
        } catch (_: Exception) {
            try {
                val currentPackageUri =
                    Uri.parse("https://play.google.com/store/apps/details?id=$marketPkg")
                val intent = Intent(Intent.ACTION_VIEW, currentPackageUri)
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                ActivityUtils.startActivity(intent)
            } catch (_: Exception) {
            }
        }
    }

    /**
     * 跳转到Facebook
     */
    fun launchFacebook(facebook: String) {
        try {
            val intent = Intent(Intent.ACTION_VIEW, Uri.parse("fb://facewebmodal/f?href=$facebook"))
            ActivityUtils.startActivity(intent)
        } catch (_: Exception) {
            try {
                val intent = Intent(Intent.ACTION_VIEW, Uri.parse(facebook))
                ActivityUtils.startActivity(intent)
            } catch (_: Exception) {
            }
        }
    }

    /**
     * 跳转到三方app
     */
    fun launchThirdApp(url: String) {
        try {
            val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
            ActivityUtils.startActivity(intent)
        } catch (_: Exception) {
        }
    }

    /**
     * 复制到剪贴板
     */
    fun copyToClipboard(context: Context, text: String, label: String = "Label") {
        val cm = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        val clipData = ClipData.newPlainText(label, text)
        cm.setPrimaryClip(clipData)
    }
}